require('dotenv').config();
const cors = require('cors');
const express = require('express');
const sequelize = require('../config/db');

const app = express();
app.use(express.json());
app.use(cors());

const authRouter = require('./routes/auth');
app.use('/api/auth', authRouter);

const categoryRouter = require('./routes/category');
const productRouter = require('./routes/product');
const orderRouter = require('./routes/order');
const userRouter = require('./routes/user');
app.use('/api/category', categoryRouter);
app.use('/api/product', productRouter);
app.use('/api/order', orderRouter);
app.use('/api/user', userRouter);
app.use('/api/product-category', require('./routes/productCategory'));
const uploadRouter = require('./routes/upload');
app.use('/api/upload', uploadRouter);
app.use('/api/uploads', express.static(require('path').join(__dirname, '../uploads')));

const setupSwagger = require('./swagger');
setupSwagger(app);

// 测试数据库连接
sequelize.authenticate()
  .then(() => {
    console.log('数据库连接成功');
    sequelize.sync();
  })
  .catch(err => console.error('数据库连接失败:', err));

// TODO: 挂载路由

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`服务已启动，端口：${PORT}`);
});
